package main

import (
	"log/slog"
	"os"

	"github.com/anthdm/hollywood/actor"
	"github.com/anthdm/hollywood/remote"

	"learn-go/hollywood/remote-benchmark/msg"
)

type server struct {
	sender *actor.PID
}

func newServer() actor.Receiver {
	return &server{}
}

func (s *server) Receive(ctx *actor.Context) {
	switch m := ctx.Message().(type) {
	case actor.Started:
		slog.Info("server started")
	case *msg.StartRemote:
		slog.Info("starting for", "m.Sender", m.Sender)
		s.sender = m.Sender
		ctx.Respond(&msg.Start{})
	case *msg.Ping:
		ctx.Send(s.sender, &msg.Pong{Count: m.Count})
	}
}

func main() {
	slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug})))
	r := remote.New("127.0.0.1:4000", remote.NewConfig())
	e, err := actor.NewEngine(actor.NewEngineConfig().WithRemote(r))
	if err != nil {
		panic(err)
	}

	e.Spawn(newServer, "server", actor.WithID("primary"))

	select {}
}
